package com.youlai.boot.modules.accounts.controller;

import com.youlai.boot.modules.accounts.service.AccountOwnerService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.modules.accounts.model.form.AccountOwnerForm;
import com.youlai.boot.modules.accounts.model.query.AccountOwnerQuery;
import com.youlai.boot.modules.accounts.model.vo.AccountOwnerVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 账户归属人前端控制层
 *
 * @author lz0816
 * @since 2025-05-23 15:10
 */
@Tag(name = "账户归属人接口")
@RestController
@RequestMapping("/api/v1/accountOwners")
@RequiredArgsConstructor
public class AccountOwnerController  {

    private final AccountOwnerService accountOwnerService;

    @Operation(summary = "账户归属人分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('modules.accounts:accountOwner:query')")
    public PageResult<AccountOwnerVO> getAccountOwnerPage(AccountOwnerQuery queryParams ) {
        IPage<AccountOwnerVO> result = accountOwnerService.getAccountOwnerPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增账户归属人")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('modules.accounts:accountOwner:add')")
    public Result<Void> saveAccountOwner(@RequestBody @Valid AccountOwnerForm formData ) {
        boolean result = accountOwnerService.saveAccountOwner(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取账户归属人表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('modules.accounts:accountOwner:edit')")
    public Result<AccountOwnerForm> getAccountOwnerForm(
        @Parameter(description = "账户归属人ID") @PathVariable Long id
    ) {
        AccountOwnerForm formData = accountOwnerService.getAccountOwnerFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改账户归属人")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('modules.accounts:accountOwner:edit')")
    public Result<Void> updateAccountOwner(
            @Parameter(description = "账户归属人ID") @PathVariable Long id,
            @RequestBody @Validated AccountOwnerForm formData
    ) {
        boolean result = accountOwnerService.updateAccountOwner(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除账户归属人")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('modules.accounts:accountOwner:delete')")
    public Result<Void> deleteAccountOwners(
        @Parameter(description = "账户归属人ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = accountOwnerService.deleteAccountOwners(ids);
        return Result.judge(result);
    }
}
